package com.hanyxx.thread;

import lombok.SneakyThrows;

/**
 * @author layman
 * @date 2021/2/8
 */
public class Demo15 {
    public static void main(String[] args) throws InterruptedException {
        /*Demo15Thread t = new Demo15Thread();
        t.start();
        System.out.println("等待子线程运算");
        //等待该线程结束
        t.join();
        System.out.println("主线程结束");*/
        Demo15ThreadA threadA = new Demo15ThreadA();
        Demo15ThreadB threadB = new Demo15ThreadB(threadA);
        Demo15ThreadC threadC = new Demo15ThreadC(threadA);

        threadB.start();
        threadC.start();

    }
}
class Demo15ThreadA extends  Thread{
    @SneakyThrows
    @Override
    public void run() {
        System.out.println("线程A开始于：" + System.currentTimeMillis());
        Thread.sleep(1000);
        System.out.println("线程A结束于：" + System.currentTimeMillis());
    }
    public synchronized void getTime(){
        System.out.println("方法执行时间： "+System.currentTimeMillis());
    }
}
class Demo15ThreadB extends Thread{
    private Demo15ThreadA threadA;
    public Demo15ThreadB(Demo15ThreadA threadA){
        this.threadA = threadA;
    }
    @SneakyThrows
    @Override
    public void run() {
        synchronized (threadA){
            threadA.start();
            //sleep会导致线程阻塞，不会释放锁
            Thread.sleep(2000L);
            //threadA.join(2000L);
            while(true){

            }
        }
    }
}
class Demo15ThreadC extends  Thread{
    private Demo15ThreadA threadA;
    public Demo15ThreadC(Demo15ThreadA threadA){
        this.threadA = threadA;
    }
    @Override
    public void run() {
        threadA.getTime();
    }
}
class Demo15Thread extends  Thread{
    @SneakyThrows
    @Override
    public void run() {
        //模拟大强度运算
        Thread.sleep(1500);
        System.out.println("子线程计算的值为： "+9584623333135895L);
    }
}
